package com.letv.core.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Formatter;
import java.util.Locale;
import java.util.TimeZone;

public class StringUtils {

    /**
     * 使用java正则表达式去掉多余的.与0
     * 
     * @param s
     * @return
     */
    public static String subZeroAndDot(String s) {
        if (s.indexOf(".") > 0) {
            s = s.replaceAll("0+?$", "");// 去掉多余的0
            s = s.replaceAll("[.]$", "");// 如最后一位是.则去掉
        }
        return s;
    }

    /**
     * 返回指定格式的时间
     * 
     * @return
     */
    public static String timeClockString(String format) {
        // 按自定义格式SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        SimpleDateFormat formatTime = new SimpleDateFormat(format);
        Date currentTime = new Date();
        return formatTime.format(currentTime);
    }

    /**
     * 生成年月日
     * 
     * @param time
     */
    public static String timeString(long time) {
        // 按自定义格式SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        SimpleDateFormat formatTime = new SimpleDateFormat("yyyy-MM-dd");
        formatTime.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
        Date currentTime = new Date(time);
        return formatTime.format(currentTime);
    }

    /**
     * 生成年月日
     * 
     * @param time
     */
    public static String timeStringByMinutes(long time) {
        SimpleDateFormat formatTime = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date currentTime = new Date(time);
        return formatTime.format(currentTime);
    }

    /**
     * 生成年月日
     * 
     * @param time
     */
    public static String timeStringBySecond(long time) {
        SimpleDateFormat formatTime = new SimpleDateFormat("MM-dd HH:mm:ss");
        Date currentTime = new Date(time * 1000);
        return formatTime.format(currentTime);
    }

    /**
     * 生成时分
     * 
     * @param time
     * @return
     */
    public static String time2StrBySec(long time) {
        SimpleDateFormat formatTime = new SimpleDateFormat("HH:mm");
        Date currentTime = new Date(time * 1000);
        return formatTime.format(currentTime);
    }

    /**
     * 生成年月日
     * 
     * @param time
     */
    public static String timeStringAll(long time) {
        // 按自定义格式SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        SimpleDateFormat formatTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date currentTime = new Date(time);
        return formatTime.format(currentTime);
    }

    /**
     * 将long型时间（单位：秒）转换为23:12:22形式
     * */
    public static String getNumberTime(long time_second) {
        Formatter formatter = new Formatter(null, Locale.getDefault());
        try {
            long seconds = time_second % 60;
            long minutes = (time_second / 60) % 60;
            long hours = time_second / 3600;
            return formatter.format("%02d:%02d:%02d", hours, minutes, seconds).toString();
        } finally {
            formatter.close();
        }
    }

    /**
     * 格式化时间字符串
     * 
     * @param timeMs 毫秒
     * @return 返回格式00:00
     */
//    public static String stringForTimeNoHour(long timeMs) {
//        StringBuilder formatBuilder = new StringBuilder();
//        Formatter formatter = new Formatter(formatBuilder, Locale.getDefault());
//        try {
//            if (timeMs >= 0) {
//                int totalSeconds = (int) (timeMs / 1000);
//
//                int seconds = totalSeconds % 60;
//                int minutes = totalSeconds / 60;
//
//                formatBuilder.setLength(0);
//
//                return formatter.format("%02d:%02d", minutes, seconds).toString();
//            } else {
//                return "00:00";
//            }
//        } finally {
//            formatter.close();
//        }
//    }

    public static String stringForTimeNoHour(long sTime) {
        int time = (int) (sTime / 1000);
        String timeStr = null;
        int hour = 0;
        int minute = 0;
        int second = 0;
        if (time <= 0)
            return "00:00";
        else {
            minute = time / 60;
            if (minute < 60) {
                second = time % 60;
                timeStr = unitFormat(minute) + ":" + unitFormat(second);
            } else {
                hour = minute / 60;
                if (hour > 99)
                    return "99:59:59";
                minute = minute % 60;
                second = time - hour * 3600 - minute * 60;
                timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
            }
        }
        return timeStr;
    }

    public static String unitFormat(int i) {
        String retStr = null;
        if (i >= 0 && i < 10)
            retStr = "0" + Integer.toString(i);
        else
            retStr = "" + i;
        return retStr;
    }

    public static long stringToLong2(String str) {
        long i = 0;
        try {
            i = Long.parseLong(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return i;
    }

    /**
     * 格式化时间字符串
     * 
     * @param timeMs 毫秒
     * @return 返回格式00:00:00
     */
    public static String stringForTime(long timeMs) {
        StringBuilder formatBuilder = new StringBuilder();
        Formatter formatter = new Formatter(formatBuilder, Locale.getDefault());

        try {
            int totalSeconds = (int) (timeMs / 1000);

            int seconds = totalSeconds % 60;
            int minutes = (totalSeconds / 60) % 60;
            int hours = totalSeconds / 3600;

            formatBuilder.setLength(0);

            return formatter.format("%02d:%02d:%02d", hours, minutes, seconds).toString();
        } finally {
            formatter.close();
        }
    }

    /**
     * 播放数转换
     * 
     * @param playCount
     * @return
     */
    public static String getPlayCountsToStr(long playCount) {
        if (playCount < 10000) // 小于10000
        {
            if (playCount < 1000) {
                return playCount + "";
            } else if (playCount >= 1000 && playCount <= 9999) {
                String aa = playCount / 1000 + "";
                String bb = playCount % 1000 + "";
                if (bb.length() < 3) {
                    bb = "0" + bb;
                }
                return aa + "," + bb;
            }
            return "";
        } else if (playCount >= 10000 && playCount < 100000000) { // 万以上 亿一下
            DecimalFormat df = new DecimalFormat(".#");
            String st = df.format(playCount * 1.0 / 10000) + "万";
            return st;
        } else { // 亿以上
            DecimalFormat df = new DecimalFormat(".#");
            String st = df.format(playCount * 1.0 / 100000000) + "亿";
            return st;
        }
    }

    /**
     * 将yyyy-MM-dd HH:mm:ss格式时间解析成指定格式
     * 
     * @param time
     * @param format
     * @return
     */
    public static String formatTime(String time, String format) {
        if (TextUtils.isEmpty(time) || TextUtils.isEmpty(format)) {
            return time;
        }
        Date date = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat df = new SimpleDateFormat(format);
        try {
            date = sdf.parse(time);
            return df.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        } finally {
            df = null;
        }
        return time;
    }

    public static boolean isToday(Date date) {
        SimpleDateFormat dfDate = new SimpleDateFormat("yyyy-MM-dd");
        return getDateName(0).equals(dfDate.format(date));
    }

    public static boolean isYesterday(Date date) {
        SimpleDateFormat dfDate = new SimpleDateFormat("yyyy-MM-dd");
        return getDateName(-1).equals(dfDate.format(date));
    }

    public static boolean isTomorrow(Date date) {
        SimpleDateFormat dfDate = new SimpleDateFormat("yyyy-MM-dd");
        return getDateName(1).equals(dfDate.format(date));
    }

    /**
     * 根据日期字符串获取周几
     * 
     * @param dateString
     * @return
     */
    public static String getWeekName(String dateString) {
        if (TextUtils.isEmpty(dateString)) {
            return dateString;
        }
        Date date = null;
        try {
            SimpleDateFormat dfDate = new SimpleDateFormat("yyyy-MM-dd");
            date = dfDate.parse(dateString);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            return getWeekName(calendar);
        } catch (ParseException e) {

            e.printStackTrace();
        }
        return dateString;
    }

    private static String getWeekName(Calendar calendar) {
        String name = calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.CHINESE);
        if (!TextUtils.isEmpty(name)) {
            return name.replace("星期", "周");
        }
        return "";
    }

    public static String getDateName(String dateString) {
        if (TextUtils.isEmpty(dateString)) {
            return dateString;
        }
        Date date = null;
        try {
            SimpleDateFormat dfDate = new SimpleDateFormat("yyyy-MM-dd");
            date = dfDate.parse(dateString);
            if (isYesterday(date)) {
                return "昨天";
            } else if (isToday(date)) {
                return "今天";
            } else if (isTomorrow(date)) {
                return "明天";
            } else {
                SimpleDateFormat dfName = new SimpleDateFormat("MM月dd日");
                return dfName.format(date);
            }

        } catch (ParseException e) {

            e.printStackTrace();
        }
        return dateString;
    }

    private static String getDateName(int i) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + i);
        SimpleDateFormat dfDate = new SimpleDateFormat("yyyy-MM-dd");
        return dfDate.format(calendar.getTime());
    }

    /**
     * yyyy-MM-dd HH:mm格式转long类型
     * 
     * @param date
     * @return
     */
    public static long getTimeLong(String date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");// 设置格式

        Date mDate;
        try {
            mDate = format.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            mDate = new Date();
        }

        return mDate.getTime();
    }

    public static String getStringTwo(String strIn) {
        String strResult = "";
        if (!TextUtils.isEmpty(strIn)) {
            if (strIn.length() >= 2) {
                strResult = strIn;
            } else {
                strResult = "0".concat(strIn);
            }
        }
        return strResult;
    }

    /**
     * 添加防盗链参数
     * 
     * @param url
     * @param isPay
     * @return
     */
//    public static String addUnlinkParams(String url, boolean isPay, String liveid) {
//        return url.concat(String.format(
//                "&platid=10&playid=1&termid=2&pay=%s&tm=1411974165&splatid=%s&ostype=andriod&hwtype=un&p1=0&p2=00&uuid=%s&liveid=%s", isPay ? 1 : 0,
//                LetvConfig.getClientId(), LetvUtils.getUUID(BaseApplication.getInstance()), liveid));
//    }

//    public static String addUnlinkParams(Context context,String url, boolean isPay, String liveid) {
//        return url.concat(String.format(
//            "&platid=10&playid=1&termid=2&pay=%s&tm=1411974165&splatid=%s&ostype=andriod&hwtype=un&p1=0&p2=00&uuid=%s&liveid=%s", isPay ? 1 : 0,
//            LetvConfig.getClientId(), LetvUtils.getUUID(context), liveid));
//    }

    /**
     * 根据token得到码流
     * 
     * @param info
     * @param token
     * @param uid
     * @return
     */
//    public static LiveStreamBean addPayTokenUid(LiveStreamBean info, String token, String uid) {
//        if (info == null) {
//            return null;
//        }
//        if (!TextUtils.isEmpty(info.getLiveUrl350()) && !info.getLiveUrl350().contains("token=")) {
//            info.setLiveUrl350(info.getLiveUrl350().concat("&token=" + token));
//            if (!info.getLiveUrl350().contains("uid=")) {
//                info.setLiveUrl350(info.getLiveUrl350().concat("&uid=" + uid));
//            }
//        }
//
//        if (!TextUtils.isEmpty(info.getLiveUrl1000()) && !info.getLiveUrl1000().contains("token=")) {
//            // info.setLiveUrl_1000(LetvLiveApi.getInstance().addUnlinkParams(info.getLiveUrl_1000(),
//            // p));
//            info.setLiveUrl1000(info.getLiveUrl1000().concat("&token=" + token));
//            if (!info.getLiveUrl1000().contains("uid=")) {
//                info.setLiveUrl1000(info.getLiveUrl1000().concat("&uid=" + uid));
//            }
//        }
//        return info;
//    }

    /**
     * 格式化开始播放时间
     * 
     * @param time
     * @return
     */
//    public static String formatPlayTime(String time) {
//        try {
//            LogInfo.log("time1 = " + time);
//            if (!TextUtils.isEmpty(time)) {
//                int dotIndex = time.indexOf(":");
//                if (dotIndex != -1) {
//                    LogInfo.log("time2 = " + time.substring(0, dotIndex + 3));
//                    return time.substring(0, dotIndex + 3);
//                }
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        return time;
//    }

    /**
     * 格式化预订时间
     * 
     * @param time
     * @return
     */
//    public static String formatBookTime(String time) {
//        if (!TextUtils.isEmpty(time) && time.length() > 16) {
//            return time.substring(0, 16);
//        } else {
//            return time;
//        }
//    }

    /**
     * 格式化预约时间
     * 
     * @param date
     * @return
     */
//    public static String getBookTime(String date) {
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");// 设置格式
//        String bookTime = "";
//
//        Date mDate;
//        try {
//            mDate = format.parse(date);
//        } catch (ParseException e) {
//            e.printStackTrace();
//            mDate = new Date();
//        }
//        Calendar mCalendar = Calendar.getInstance();
//        mCalendar.setTime(mDate);
//
//        SimpleDateFormat formatTime = new SimpleDateFormat("HH:mm");
//        bookTime = formatTime.format(mCalendar.getTime());
//
//        return bookTime;
//    }

    /**
     * 获取星期名称
     * 
     * @param date
     * @return
     */
//    public static String getWeekDayName(String date) {
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");// 设置格式
//        String weekDayName = "";
//
//        Date mDate;
//        try {
//            mDate = format.parse(date);
//        } catch (ParseException e) {
//            e.printStackTrace();
//            mDate = new Date();
//        }
//        Calendar mCalendar = Calendar.getInstance();
//        mCalendar.setTime(mDate);
//
//        int week_day = mCalendar.get(Calendar.DAY_OF_WEEK);
//        SimpleDateFormat formatTime = new SimpleDateFormat("MM月dd日");
//        weekDayName = formatTime.format(mCalendar.getTime()) + "(" + getWeek(week_day) + ")";
//
//        return weekDayName;
//    }

    /**
     * 根据日期得到的星期转换为正常的星期(星期日的值为1)
     * 
     * @param week_day
     * @return
     */
//    public static String getWeek(int week_day) {
//
//        String week = "";
//
//        switch (week_day) {
//            case 2:
//                week = "周一";
//                break;
//            case 3:
//                week = "周二";
//                break;
//            case 4:
//                week = "周三";
//                break;
//            case 5:
//                week = "周四";
//                break;
//            case 6:
//                week = "周五";
//                break;
//            case 7:
//                week = "周六";
//                break;
//            case 1:
//                week = "周日";
//                break;
//            default:
//                break;
//        }
//        return week;
//    }

    /**
     * 直播标题
     * 
     * @param channel_Num
     * @param channel_Name
     * @param program_Name
     * @return
     */
//    public static String formatLiveTitle(String channel_Num, String channel_Name, String program_Name) {
//        if (!TextUtils.isEmpty(channel_Name)) {
//            StringBuilder builder = new StringBuilder();
//            if (!TextUtils.isEmpty(channel_Num) && channel_Num.length() == 1) {
//                builder.append("0");
//                builder.append(channel_Num).append("  ");
//            } else if (!TextUtils.isEmpty(channel_Num)) {
//                builder.append(channel_Num).append("  ");
//            }
//            builder.append(channel_Name);
//            if (!TextUtils.isEmpty(program_Name)) {
//                builder.append("：").append(program_Name);
//            }
//            return builder.toString();
//        }
//        return program_Name;
//    }

    // ------------用指定颜色高亮显示字符串中指定字符-----
    /*
     * function 高亮显示指定字符串
     * 
     * @param1 inputString 输入字符串
     * 
     * @param2 highlight 需要高亮显示字符串
     * 
     * @param3 color 高亮显示字符串颜色
     */
//    public static SpannableString highlightParamText(String inputString, String highlight, int color) {
//        inputString = String.format(inputString, highlight);
//        SpannableString spanString = new SpannableString(inputString);
//        if (!TextUtils.isEmpty(inputString)) {
//            int beginPos = inputString.indexOf(highlight);
//            if (beginPos > -1) {
//                spanString.setSpan(new ForegroundColorSpan(color), beginPos, beginPos + highlight.length(),
//                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//            }
//        }
//        return spanString;
//    }

    /*
     * function 高亮显示指定字符串
     * 
     * @param1 inputString 输入字符串
     * 
     * @param2 highlight 需要高亮显示字符串
     * 
     * @param3 color 高亮显示字符串颜色
     */
//    public static SpannableString highlightParamText(String inputString, int index, String highlight, int color) {
//        inputString = String.format(inputString, highlight);
//        SpannableString spanString = new SpannableString(inputString);
//        if (!TextUtils.isEmpty(inputString)) {
//            int beginPos = index;
//            if (beginPos > -1) {
//                spanString.setSpan(new ForegroundColorSpan(color), beginPos, beginPos + highlight.length(),
//                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//            }
//        }
//        return spanString;
//    }

    /*
     * function 高亮显示指定字符串（格式化/非格式化操作）
     * 
     * @param1 inputString 输入字符串
     * 
     * @param2 highlight 需要高亮显示字符串
     * 
     * @param3 color 高亮显示字符串颜色
     * 
     * @param4 formatParam 是否需要对字符串进行格式化操作，true:对输入字符串进行格式化 false:需要高亮现实的字符串已存在于inputstring中
     */
//    public static SpannableString highlightParamText(String inputString, String highlight, int color,
//            boolean formatParam) {
//        if (formatParam) {
//            inputString = String.format(inputString, highlight);
//        }
//        SpannableString spanString = new SpannableString(inputString);
//        if (!TextUtils.isEmpty(inputString)) {
//            int beginPos = inputString.indexOf(highlight);
//            if (beginPos > -1) {
//                spanString.setSpan(new ForegroundColorSpan(color), beginPos, beginPos + highlight.length(),
//                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//            }
//        }
//        return spanString;
//    }

    /*
     * function 高亮显示指定字符串 于输入字符串末尾
     * 
     * @param1 inputString 输入字符串
     * 
     * @param2 highlight 需要高亮显示字符串
     * 
     * @param3 color 高亮显示字符串颜色
     */
//    public static SpannableString highlightParamTextAdded(String inputString, String highlight, int color) {
//        int beginPos = inputString.length();
//        inputString = inputString + highlight;
//        SpannableString spanString = new SpannableString(inputString);
//        spanString.setSpan(new ForegroundColorSpan(color), beginPos, beginPos + highlight.length(),
//                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//        return spanString;
//    }

    /*
     * function 高亮显示指定字符串 及之后一位的字符
     * 
     * @param1 inputString 输入字符串
     * 
     * @param2 highlight 需要高亮显示字符串
     * 
     * @param3 color 高亮显示字符串颜色
     */
//    public static SpannableString highlightParamTextOffsetRightOne(String inputString, String highlight, int color) {
//        inputString = String.format(inputString, highlight);
//        SpannableString spanString = new SpannableString(inputString);
//        if (!TextUtils.isEmpty(inputString)) {
//            int beginPos = inputString.indexOf(highlight);
//            if (beginPos > -1) {
//                int endPos = (beginPos + highlight.length() + 1) > inputString.length() ? inputString.length()
//                        : (beginPos + highlight.length() + 1);
//                spanString.setSpan(new ForegroundColorSpan(color), beginPos, endPos, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//            }
//        }
//        return spanString;
//    }

    /*
     * function 高亮显示指定字符串 及之后一位的字符
     * 
     * @param1 inputString 输入字符串
     * 
     * @param2 highlight 需要高亮显示数字
     * 
     * @param3 color 高亮显示字符串颜色
     */
    public static SpannableString highlightParamTextOffsetRightOne(String inputString, int highlight, int color) {
        inputString = String.format(inputString, highlight);
        String highlightValue = Integer.toString(highlight);
        SpannableString spanString = new SpannableString(inputString);
        if (!TextUtils.isEmpty(inputString)) {
            int beginPos = inputString.indexOf(highlightValue);
            if (beginPos > -1) {
                int endPos = (beginPos + highlightValue.length() + 1) > inputString.length() ? inputString.length()
                        : (beginPos + highlightValue.length() + 1);
                spanString.setSpan(new ForegroundColorSpan(color), beginPos, endPos, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        return spanString;
    }

    /*
     * function 高亮显示指定字符串之间的字符
     * 
     * @param1 inputString 输入字符串 String
     * 
     * @param2 beforeHighlight 需要高亮字符串 之前的字符
     * 
     * @param3 afterHighlight 需要高亮字符串 之后的字符
     * 
     * @param4 color 高亮显示字符串颜色
     */
    public static SpannableString highlightParamTextBetweenTwoString(String inputString, String beforeHighlight,
            String afterHighlight, int color) {
        SpannableString spanString = new SpannableString(inputString);
        return highlightParamTextBetweenTwoString(spanString, beforeHighlight, afterHighlight, color);
    }

    /*
     * function 高亮显示指定字符串之间的字符
     * 
     * @param1 inputString 输入字符串 SpannableString
     * 
     * @param2 beforeHighlight 需要高亮字符串 之前的字符
     * 
     * @param3 afterHighlight 需要高亮字符串 之后的字符
     * 
     * @param4 color 高亮显示字符串颜色
     */
    public static SpannableString highlightParamTextBetweenTwoString(SpannableString spanString,
            String beforeHighlight, String afterHighlight, int color) {
        String inputString = spanString.toString();
        if (!TextUtils.isEmpty(inputString)) {
            int beginPos = inputString.indexOf(beforeHighlight) + beforeHighlight.length();
            if (beginPos > -1) {
                int endPos = inputString.indexOf(afterHighlight);
                if (endPos > -1 && beginPos < endPos) {
                    spanString.setSpan(new ForegroundColorSpan(color), beginPos, endPos,
                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
        }
        return spanString;
    }

    /*
     * function 放大/缩小显示指定字符串中的字符字体大小
     * 
     * @param1 spanString 输入字符串 SpannableString
     * 
     * @param2 index 放大/缩小需要指定字符串的索引
     * 
     * @param3 length 指定索引字符串之后需要放大显示字符个数
     * 
     * @param4 size 字体大小
     */
    public static SpannableString highlightSpecifyIndexLengthTextSize(SpannableString spanString, int size, int index,
            int length) {
        SpannableString spanSizeString = spanString;
        spanSizeString.setSpan(new AbsoluteSizeSpan(size, true), index, length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spanSizeString;
    }

//    public static String getString(int id) {
//        return BaseApplication.getInstance().getString(id);
//    }

//    public static String getString(int id, Object... objects) {
//        return BaseApplication.getInstance().getString(id, objects);
//    }

    /**
     * 从本地获取今天是周几()
     * 
     * @return 1 代表周一，依次类推
     */
    public static int getLocalWeekDay() {
        Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
        Date date = new Date(System.currentTimeMillis());
        return date.getDay() == 0 ? 7 : date.getDay();
    }

    /**
     * 播放缓冲统计 时间格式化xx.xx（单位秒）
     * 
     * @param time
     */
    public static float staticticsLoadTimeInfoFormat(long time) {
        if (time > 575990000) {
            time = System.currentTimeMillis() - time;
        }
        return (time / 10) * 1.0f / 100;
    }
    /**
     * 播放缓冲统计 时间格式化xx.xx（单位秒）
     * 
     * @param time
     */
    public static float staticticsLoadTimeInfoFormat(long time1, long time2) {
        long time = 0;
        if (time1 > 575990000) {
            time1 = System.currentTimeMillis() - time1;
        }
        if (time2 > 575990000) {
            time2 = System.currentTimeMillis() - time2;
        }
        time = time1 > time2 ? time1 : time2;
        return (time / 10) * 1.0f / 100;
    }


    /**
     * 返回当前时间的时:分 "HH:mm"
     * 
     * @return
     */
    public static String timeClockString() {
        // 按自定义格式SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        SimpleDateFormat formatTime = new SimpleDateFormat("HH:mm");
        Date currentTime = new Date();
        return formatTime.format(currentTime);
    }

    /**
     * 计算time2 - time1的值,比如：“2014-08-04” - “2014-08-01” = 3；
     * 
     * @return
     */
    public static int daysBetween(String t1, String t2) throws ParseException {
        if (t1.length() > 10) {
            t1 = t1.substring(0, 10);
        }

        if (t2.length() > 10) {
            t2 = t2.substring(0, 10);
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(t1));
        long time1 = cal.getTimeInMillis();
        cal.setTime(sdf.parse(t2));
        long time2 = cal.getTimeInMillis();
        long betweenDays = (time2 - time1) / (1000 * 3600 * 24);
        LogInfo.log("save_", "t1 = " + t1 + " , t2 = " + t1 + ", time1 = " + time1 + " , time2 = " + time2
                + " , time2 - time1 = " + (time2 - time1) + " , betweenDays = " + betweenDays);
        return Integer.parseInt(String.valueOf(betweenDays));
    }

    /**
     * 计算HH:mm 相减的分钟差
     * 
     * @param m1
     * @param m2
     * @return
     */
    public static int calMinuteBetween(String m1, String m2) {
        int t1 = (Integer.valueOf(m1.substring(0, 2)) - Integer.valueOf(m2.substring(0, 2))) * 60;
        int t2 = Integer.valueOf(m1.substring(3, 5)) - Integer.valueOf(m2.substring(3, 5));
        return t1 + t2;
    }

    /**
     * 生成超级电视链接文字
     * 
     * @param text
     * @param clickableSpan
     * @return
     */
    public static SpannableString generateTvSpreadSpanText(String text, ClickableSpan clickableSpan) {
        SpannableString spannableString = new SpannableString(text);
        if (clickableSpan != null) {
            spannableString.setSpan(clickableSpan, text.length() - 5, text.length(),
                    SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return spannableString;
    }

    public static int stringToLong(String time) {
        String[] times = time.split(":");
        if (times.length == 3) {
            int hour = Integer.parseInt(times[0]);
            int minute = Integer.parseInt(times[1]);
            int second = Integer.parseInt(times[2]);
            return hour * 60 * 60 + minute * 60 + second;
        } else if (times.length == 2) {
            int minute = Integer.parseInt(times[0]);
            int second = Integer.parseInt(times[1]);
            return minute * 60 + second;
        } else if (times.length == 1) {
            if (!TextUtils.isEmpty(times[0])) {
                return Integer.parseInt(times[0]);
            }
        }
        return 0;
    }

    public static long stringToMillisecond(String time) {
        return stringToLong(time) * 1000;
    }

    @SuppressLint("SimpleDateFormat")
    public static String getTimeStamp() {
        // 按自定义格式SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 10:10:10.333 yyyyMMdd HH:mm:ss.SSS
        SimpleDateFormat formatTime = new SimpleDateFormat("yyyyMMdd HH:mm:ss.SSS");
        formatTime.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
        Date currentTime = new Date(System.currentTimeMillis());
        String tt = formatTime.format(currentTime);
        return tt;
    }
}
